#include <stdio.h>
#define SIZE 15
#define true 1
#define false  0
void initboard(char (*board)[SIZE])
{
    for(int i=0;i<SIZE;i++)
    {
        for(int j=0;j<SIZE;j++)
        {
            board[i][j]='-';
        }
    }
}
void printboard(char (*board)[SIZE])
{
    printf("  ");
    for(int i=0;i<SIZE;i++)
    {
        printf(" %2d",i+1);
    }
    printf("\n");
    for(int i=0;i<SIZE;i++)
    {
        printf("%2d ",i+1);
        for(int j=0;j<SIZE;j++)
        {
            printf("%2c ",board[i][j]);
        }
        printf("\n");
    }

}
int makemove(char (*board)[SIZE],int row,int colum,char player)
{
    if(row < 0 || row >=SIZE|| colum < 0 || colum >=SIZE)
    {
        printf("invald place!please try again!\n");
        return false;
    }
    if(board[row][colum]!='-')
    {
        printf("this place have no place!try again!\n");
        return false;
    }
    
    board[row][colum]=player;
    return true;

}
int victoryboard(char (*board)[SIZE],int row,int colum,char player)
{
    int direction[4][2]={{1,0},{0,1},{1,1},{-1,1}};
    for(int i=0;i<4;i++)
    {
        int count=1;
        int dx=row+direction[i][0];
        int dy=colum+direction[i][1];
        while(dx>=0&&dx<SIZE&dy>=0&&dy<SIZE&&board[dx][dy]==player)
        {
            count++;
            dx=dx+direction[i][0];
            dy=dy+direction[i][1];
            if(count==5)
                return true;
        }
        dx=row-direction[i][0];
        dy=colum-direction[i][1];
        while(dx>=0&&dx<SIZE&dy>=0&&dy<SIZE&&board[dx][dy]==player)
        {
            count++;
            dx=dx-direction[i][0];
            dy=dy-direction[i][1];
            if(count==5)
                return true;
        }
        if(count>=5)
            return true;
    }
    return false;
}
int checkisdraw(char (*board)[SIZE])
{
    for(int i=0;i<SIZE;i++)
    {
        for(int j=0;j<SIZE;j++)
        {
            if(board[i][j]=='-')
                return false;
        }
    }
    return true;
}
int main()
{
    char board[SIZE][SIZE]={0};
    initboard(board);
    char player='X';
    while(1)
    {
        printboard(board);
        int row=0,colum=0;
        printf("please %c input a place to mae move:",player);
        scanf("%d %d",&row,&colum);
        if(makemove(board,row-1,colum-1,player)==false)
        {
            continue;
        }
        if(victoryboard(board,row-1,colum-1,player)==true)
        {
            printboard(board);
            printf("player %c victory\n",player);
            break;
        }
        if(checkisdraw(board)==true)
        {
            printboard(board);
            printf("draw game over\n");
            break;
        }
        player=((player=='X')?'O':'X');
    }


    return 0;
}